package com.testing.class14;

import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @Classname CookieTest
 * @Description 类型说明
 * @Date 2022/11/4 20:10
 * @Created by 特斯汀Roy
 */
public class CookieTest {
    public static void main(String[] args) throws IOException {
        CloseableHttpClient roy= HttpClients.createDefault();
        //先登录去获取cookie
        HttpPost login=new HttpPost("http://localhost:9090/user/login");
        StringEntity loginParam=new StringEntity("username=will&password=123456","utf-8");
        loginParam.setContentType("application/x-www-form-urlencoded");
        loginParam.setContentEncoding("utf-8");
        login.setEntity(loginParam);
        //手动把返回结果的cookie拿出来。
        CloseableHttpResponse loginResponse = roy.execute(login);
        System.out.println(loginResponse);
        //获取返回中的cookie
        Header[] headers = loginResponse.getHeaders("Set-Cookie");
        System.out.println("-----------------------返回结果中的头域--------------------------------");
        String royCookie="";
        for (Header header : headers) {
            System.out.println(header.getName()+":"+header.getValue());
            System.out.println(header.getValue().substring(0,header.getValue().indexOf(";")));
            royCookie=header.getValue().substring(0,header.getValue().indexOf(";"));
        }

        //JSESSIONID=A8C159406C2CDE90BC2CF7695E5EE061; Max-Age=120; Expires=Fri, 04-Nov-2022 12:41:25 GMT; Path=/


        String loginRes = EntityUtils.toString(loginResponse.getEntity(), "utf-8");
        System.out.println(loginRes);

        System.out.println("------------------用同一个client直接请求------------------");
        //使用cookie，添加cookie头，完成getUser调用。
        HttpGet getUser=new HttpGet("http://localhost:9090/user/getUser");
//        getUser.setHeader("Cookie","JSESSIONID=77478DCBBA2CAA66066444A99FE1D198");
//        getUser.setHeader("User-Agent","ozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36");
        String s = EntityUtils.toString(roy.execute(getUser).getEntity(), "utf-8");
        System.out.println(s);

        //不带cookie的时候，返回结果是不是您还没有登录。
        System.out.println("---------------------这一次换一个client直接请求-------------------------");
        CloseableHttpClient will = HttpClients.createDefault();
        HttpGet getUser2=new HttpGet("http://localhost:9090/user/getUser");
//        getUser2.setHeader("Cookie","JSESSIONID=77478DCBBA2CAA66066444A99FE1D198");
//        getUser.setHeader("User-Agent","ozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36");
        String user2 = EntityUtils.toString(will.execute(getUser2).getEntity(), "utf-8");
        System.out.println(user2);


        System.out.println("---------------带上获取的cookie来进行请求-------------------");
        //用另一个httpclient客户端，携带roy的cookie去进行请求。
        HttpGet getUserWithRoyCookie=new HttpGet("http://localhost:9090/user/getUser");
        getUserWithRoyCookie.setHeader("Cookie",royCookie);
//        getUser.setHeader("User-Agent","ozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36");
        String userWithRoyCookie = EntityUtils.toString(will.execute(getUserWithRoyCookie).getEntity(), "utf-8");
        System.out.println(userWithRoyCookie);


    }
}
